package com.yht.downloadimage.thread;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.CloseableHttpClient;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;

import com.yht.downloadimage.download.file.DownLoadFile;
import com.yht.downloadimage.httpClient.HttpClientManager;
import com.yht.downloadimage.storage.NumStorage;
import com.yht.downloadimage.util.StaticClazz;
import com.yht.downloadimage.webdriver.GetWebDriver;
public class DownLoadThread implements Runnable {
	private long sleepTime;
	private String url;
	private String dir;
	private String houzhui;
	private String ip;
	private int port;
	private boolean webDriver;
	private String webDriverType;
	public void setWebDriverType(String webDriverType) {
		this.webDriverType = webDriverType;
	}
	public void setWebDriver(boolean webDriver) {
		this.webDriver = webDriver;
	}
	public String getIp() {
		return ip;
	}
	public void setIp(String ip) {
		this.ip = ip;
	}
	public int getPort() {
		return port;
	}
	public void setPort(int port) {
		this.port = port;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public String getDir() {
		return dir;
	}
	public void setDir(String dir) {
		this.dir = dir;
	}
	public String getHouzhui() {
		return houzhui;
	}
	public void setHouzhui(String houzhui) {
		this.houzhui = houzhui;
	}
	public long getSleepTime() {
		return sleepTime;
	}
	public void setSleepTime(long sleepTime) {
		this.sleepTime = sleepTime;
	}
	@Override
	public void run() {
		int num=NumStorage.getNum();
		if(num==0){
			System.out.println("是否初始化[NumStorage]!!!");
		}
		while(num>0){
			try {
				if(webDriver){
					downloadByWebdriver(num);
				}else{
					downloadByHttpClient(num);
				}
				Thread.sleep(sleepTime);
			} catch (Exception e) {
				NumStorage.addNum(num);
				e.printStackTrace();
				continue;
			}
			num=NumStorage.getNum();
		}
		
	}
	
	private void downloadByHttpClient(int num) throws Exception{
		CloseableHttpClient httpClient = HttpClientManager.getConnection();
		DownLoadFile dlf=new DownLoadFile();
		 RequestConfig config =null;
		if(ip!=null&&ip.trim().length()!=0){
			HttpHost proxy = new HttpHost(ip.trim(),port);  
			config=RequestConfig.custom().setProxy(proxy).build(); 
		}
		Map<String,String> m=new HashMap<String,String>();
		m.put("User-Agent", "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:41.0) Gecko/20100101 Firefox/41.0");
		m.put("Set-Cookie", "QINGCLOUDELB=e60a619f2dccb1d9d94d3b2c9dcbe663d562b81daa2a899c46d3a1e304c7eb2b|WGJtf|WGJrC; path=/");
		dlf.downloadImage(httpClient, url, config, m, dir+"/"+num+houzhui);
	}
	
	private void downloadByWebdriver(int num) throws Exception {
		if(webDriverType==null||(!webDriverType.equals(StaticClazz.CHROME)&&!webDriverType.equals(StaticClazz.PHANTOMJS)&&!webDriverType.equals(StaticClazz.FIREFOX))){
			System.out.println("未指定浏览器类型.....................");
			return;
		}
		WebDriver webDriver=null;
		try {
			if(webDriverType.equals(StaticClazz.CHROME)){
				webDriver=GetWebDriver.getChromeDriver("");
			}else if(webDriverType.equals(StaticClazz.FIREFOX)){
				webDriver=GetWebDriver.getFireFoxDriver("");
			}else if(webDriverType.equals(StaticClazz.PHANTOMJS)){
				webDriver=GetWebDriver.getPhantomJSDriver("");
			}
			webDriver.get(url);
			TakesScreenshot ts=(TakesScreenshot)webDriver.findElement(By.xpath("img"));
			File screenshotAs = ts.getScreenshotAs(OutputType.FILE);
			FileUtils.copyFile(screenshotAs, new File(dir+"/"+num+houzhui));
		} catch (Exception e) {
			throw e;
		}finally {
			if(webDriver!=null){
				webDriver.quit();
			}
		}
		
	}
}
